
package com.xinan.weixin.resource;

import com.jfinal.aop.Before;
import com.jfinal.aop.ClearInterceptor;
import com.jfinal.aop.ClearLayer;
import com.jfinal.core.BaseController;
import com.jfinal.core.Const;
import com.jfinal.core.Controller;
import com.jfinal.ext.interceptor.SearchSql;
import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.xinan.utils.Constants;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class WxResourceController extends BaseController {

    public void index() {
        render("/WEB-INF/views/weixin/resource/wxResource.html");
    }
/**
     * add
     */
    public void  newModes() {
        String   id=getPara("id");
        String nr="";
//    List<Record> newlist = Db.find("select  * from     where    id=  '" + id + "'");
        List<Record> newlist =  WxResource.dao.findAlls(  "       id=  '" + id + "'") ;
        if(newlist.size()>0){
            nr=newlist.get(0).get("nr");
        }
        setAttr("nr",nr);
        setAttr("newlist",newlist);
        render("/WEB-INF/views/weixin/resource/wxResource_form.html");
    }


    /**
     * add
     */
    @ClearInterceptor(ClearLayer.ALL)
    public void  view() {
        String ids=getPara(0);
        String   id=getPara("id");
        if(ids!=null){
            id=ids;
        }
        String nr="";
        List<Record> newlist =  WxResource.dao.findAlls(  "       id=  '" + id + "'") ;
        if(newlist.size()>0){
            nr=newlist.get(0).get("nr");
        }
        setAttr("nr",nr);
        setAttr("newlist",newlist);
       render("/WEB-INF/views/weixin/resource/view.html");
    }
   /**
     * 在增加了SearchSql拦截之后就可以根据页面配置信息
     * 自动装配查询语句放入request中,通过getAttr(Const.SEARCH_SQL)函数获得
     * 根据实际需要自动组装到Sql语句里面
     */
    @Before(SearchSql.class)
	public void query() {
        int pageNumber = getParaToInt("page");
        int pageSize = getParaToInt("rows");
        String where = getAttr(Const.SEARCH_SQL);
        Page<WxResource> page = WxResource.dao.page(pageNumber, pageSize, where);
        Map root = new HashMap();
		// / jqgrid用 分页显示
		root.put("total", page.getTotalPage());
		root.put("page", page.getPageNumber());
		root.put("records", page.getTotalRow());
		root.put("rows", page.getList());
		renderJson(JsonKit.toJson(root));
	}
	/**
     * save
     * @param
     * @return
     */
	public void save() {
        if(getModel(WxResource.class).save()){
        	renderText(Constants.SAVESUCCESS);
        }else{
        	renderText(Constants.SAVEFAILED);
        }
	}

/**
     * Post请求的菜单更新函数,可以接受菜单的id和菜单对象,并且根据菜单id进行更新,返回影响行数根据此判断是否操作成功
     * 本例子使用rose框架的est风格url可以吧参数作为路径一部分进行传递
     * @param
     * @return
     */
	public void update() {
        if(getModel(WxResource.class).update()){
        	renderText(Constants.EDITSUCCESS);
        }else{
        	renderText(Constants.EDITFAILED);
        }
	}

		/**
     * Post请求的菜单删除函数,可以接受菜单ID 并且根据ID进行物理删除,并且返回影响行数,据此判断是否操作成功
     * @param
     * @return
     */
	public void delete() {
        if(WxResource.dao.deleteById(getPara("id"))){
        	renderText(Constants.DELETESUCCESS);
        }else{
        	renderText(Constants.DELETEFAILED);
        }
	}	
}

